package com.qyhl.module_practice.score.shop.detail;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.gyf.immersionbar.ImmersionBar;
import com.qyhl.module_practice.R;
import com.qyhl.module_practice.R2;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.basiclib.utils.NetUtil;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.civilized.PracticeShopListBean;
import com.qyhl.webtv.commonlib.utils.ResizableImageView;
import com.qyhl.webtv.commonlib.utils.action.ActionConstant;
import com.qyhl.webtv.commonlib.utils.action.ActionLogUtils;
import com.qyhl.webtv.commonlib.utils.eventbus.BusFactory;
import com.qyhl.webtv.commonlib.utils.eventbus.Event;
import com.weavey.loading.lib.LoadingLayout;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ARouterPathConstant.PRACTICE_SCORE_SHOP_DETAIL)
public class PracticeScoreShopDetailActivity extends BaseActivity implements PracticeScoreShopDetailContract.PracticeScoreShopDetailView {
    @BindView(R2.id.cover)
    ResizableImageView cover;
    @BindView(R2.id.score)
    TextView score;
    @BindView(R2.id.exchange_num)
    TextView exchangeNum;
    @BindView(R2.id.stock_num)
    TextView stockNum;
    @BindView(R2.id.name)
    TextView name;
    @BindView(R2.id.address)
    TextView address;
    @BindView(R2.id.phone)
    TextView phone;
    @BindView(R2.id.recycle_view)
    RecyclerView recycleView;
    @BindView(R2.id.detail_layout)
    LinearLayout detailLayout;
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;
    @BindView(R2.id.exchange_btn)
    TextView exchangeBtn;
    @BindView(R2.id.summary)
    TextView summary;

    private PracticeScoreShopDetailPresenter mPresenter;

    private String itemId;

    private String volId;

    private String userName;

    private List<PracticeShopListBean.ShopDetailImage> imgList = new ArrayList<>();

    private CommonAdapter commonAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected int getContentView() {
        return R.layout.practice_activity_score_shop_detail;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {
        setDefaultImmerBar();
    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @Override
    protected void init() {
        mPresenter = new PracticeScoreShopDetailPresenter(this);
        itemId = getIntent().getStringExtra("id");
        volId = getIntent().getStringExtra("volId");
        userName = getIntent().getStringExtra("userName");
        initView();


    }

    @Override
    protected void setListener() {
        setListeners();
        mPresenter.getDetail(itemId);
    }

    private void initView() {
        loadMask.setStatus(LoadingLayout.Loading);

        recycleView.setLayoutManager(new LinearLayoutManager(this));
        recycleView.setAdapter(commonAdapter = new CommonAdapter<PracticeShopListBean.ShopDetailImage>(this, R.layout.practice_item_score_shopping_detail, imgList) {
            @Override
            protected void convert(ViewHolder holder, PracticeShopListBean.ShopDetailImage bean, int position) {
                ResizableImageView cover = holder.getView(R.id.cover);

                Glide.with(PracticeScoreShopDetailActivity.this)
                        .load(bean.getUrl())
                        .apply(new RequestOptions().placeholder(R.drawable.cover_normal_default).error(R.drawable.cover_normal_default))
                        .into(cover);
            }

        });
    }


    private void setListeners() {
        loadMask.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadMask.setReloadButtonText("加载中...");
                mPresenter.getDetail(itemId);
            }
        });
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void setDetail(PracticeShopListBean bean) {
        loadMask.setStatus(LoadingLayout.Success);
        loadMask.setReloadButtonText("点击重试~");

        Glide.with(this.getApplicationContext())
                .load(bean.getItemPic())
                .apply(new RequestOptions().placeholder(R.drawable.cover_normal_default).error(R.drawable.cover_normal_default))
                .into(cover);

        score.setText(bean.getPoint() + "");

        name.setText(bean.getItemName());

        summary.setText(bean.getItemDesc());

        exchangeNum.setText(bean.getReceipt() + "人已兑换");

        stockNum.setText("库存：" + bean.getStock());

        if (bean.getTotal() < 1) {
            exchangeBtn.setEnabled(false);
        } else {
            exchangeBtn.setEnabled(true);
        }

        if (bean.getShop() != null) {
            address.setText("兑换地址：" + bean.getShop().getAddress());

            phone.setText("商家电话：" + bean.getShop().getPhone());
        }

        imgList.clear();
        if (bean.getImgs() != null && bean.getImgs().size() > 0) {
            detailLayout.setVisibility(View.VISIBLE);
            imgList.addAll(bean.getImgs());
        } else {
            detailLayout.setVisibility(View.GONE);
        }
        commonAdapter.notifyDataSetChanged();
    }

    @Override
    public void setError(String msg) {
        loadMask.setStatus(LoadingLayout.Error);
        loadMask.setReloadButtonText("点击重试~");
        if (!NetUtil.isNetworkAvalible(this)) {
            loadMask.setErrorImage(R.drawable.error_network);
            loadMask.setErrorText("网络异常，请检查您的网络！");
        } else if (msg.contains("暂无")) {
            loadMask.setErrorImage(R.drawable.empty_content);
            loadMask.setErrorText(msg);
        } else {
            loadMask.setErrorImage(R.drawable.error_content);
            loadMask.setErrorText(msg);
        }
    }

    @Override
    public void exchangeSuccess(String msg) {
        showToast(msg, 4);
        mPresenter.getDetail(itemId);
        BusFactory.getBus().post(new Event.PracticeScoreRefresh());
    }

    @Override
    public void exchangeError(String msg) {
        showToast(msg, 4);
        mPresenter.getDetail(itemId);
    }

    @OnClick({R2.id.back_btn, R2.id.exchange_btn})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.back_btn) {
            finish();
        } else if (id == R.id.exchange_btn) {
            if (volId.equals("0")) {
                showToast("积分不足，赶快成为志愿者赚取积分吧！", 4);
            } else {
                mPresenter.doExchange(itemId, volId, userName);
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        ActionLogUtils.getInstance().onResume(this, ActionConstant.PRACTICE_SHOPPING_DETAIL);
    }

    @Override
    protected void onPause() {
        super.onPause();
        ActionLogUtils.getInstance().onPause(this, ActionConstant.PRACTICE_SHOPPING_DETAIL);
    }
}
